package smodul

import (
	"gitee.com/yutiandou/wdtools/wdlog"
	"github.com/jinzhu/gorm"
)

type BroadcastMsg struct {
	gorm.Model
	Rid uint32 `gorm:"not null"`
	CourseId uint32 `gorm:"not null"`
	MsgType uint32 `gorm:"not null"`
	Content string `gorm:"type:varchar(1024)"`
	Length int
	Title string `gorm:"type:varchar(1024)"`
	Url string `gorm:"type:varchar(1024)"`
	Description string `gorm:"type:varchar(2048)"`
	ThumbUrl string  `gorm:"type:varchar(2048)"`
	AppName string `gorm:"type:varchar(255)"`
	ThumbAesKey string `gorm:"type:varchar(255)"`
	PagePath string `gorm:"type:varchar(2048)"`
}

//AddMsgToMysql 向数据库添加一条消息
func AddMsgToMysql(rid,cid,mt uint32,ctx string,len int)bool{
	bc := & BroadcastMsg{
		Model:       gorm.Model{},
		Rid:         rid,
		CourseId:    cid,
		MsgType:	 mt,
		Content:     ctx,
		Length:      len,
		Title:       "",
		Url:         "",
		Description: "",
		ThumbUrl:    "",
		AppName:     "",
		ThumbAesKey: "",
		PagePath:    "",
	}
	if err := MysqlDB.Create(bc).Error;wdlog.Error(err){
		return false
	}
	return true
}

func GetAllMsgBy_CourseId(cid uint32)([]BroadcastMsg,int){
	bcs := make([]BroadcastMsg,0)
	rows:= MysqlDB.Where("course_id = ?", cid).Find(&bcs).RowsAffected
	return bcs,int(rows)
}